<template>
	<div class="app-container">
		<!-- 搜索区域 -->
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
			class="search-form">
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"
					class="search-input" />
			</el-form-item>
			<el-form-item label="服务类别" prop="type">
				<el-select v-model="queryParams.type" placeholder="请选择服务类别" class="search-select">
					<el-option label="服务" value="1"></el-option>
					<el-option label="娱乐" value="2"></el-option>
					<el-option label="运动" value="3"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="标签" prop="tag">
				<el-input v-model="queryParams.tag" placeholder="请输入标签" clearable @keyup.enter.native="handleQuery"
					class="search-input" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- 操作按钮区域 -->
		<el-row :gutter="10" class="mb8 operation-bar">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['service:service:add']">
					新增
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['service:service:edit']">
					修改
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['service:service:remove']">
					删除
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['service:service:export']">
					导出
				</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<!-- 表格区域 -->
		<el-table v-loading="loading" :data="serviceList" @selection-change="handleSelectionChange" border stripe
			highlight-current-row class="service-table">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="编号ID" align="center" prop="id" width="80" />
			<el-table-column label="服务类别" align="center" prop="type" width="100">
				<template slot-scope="scope">
					<el-tag :type="scope.row.type === 1 ? 'primary' : (scope.row.type === 2 ? 'success' : 'warning')">
						{{ scope.row.type === 1 ? '服务' : (scope.row.type === 2 ? '娱乐' : '运动') }}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="图片" align="center" prop="image" width="100">
				<template slot-scope="scope">
					<image-preview :src="scope.row.image" :width="50" :height="50" class="service-image" />
				</template>
			</el-table-column>
			<el-table-column label="名称" align="center" prop="name" min-width="120" />
			<el-table-column label="标签" align="center" prop="tag" min-width="120">
				<template slot-scope="scope">
					<el-tag v-for="tag in scope.row.tag.split(',')" :key="tag" type="info" size="mini" class="tag-item">
						{{ tag }}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="选择人数" align="center" prop="number" width="100" />
			<el-table-column label="项目详情" align="center" min-width="200" show-overflow-tooltip>
				<template slot-scope="scope">
					<div class="ellipsis-text">{{ delHtmlTags(scope.row.details) }}</div>
				</template>
			</el-table-column>
			<el-table-column label="预定须知" align="center" min-width="200" show-overflow-tooltip>
				<template slot-scope="scope">
					<div class="ellipsis-text">{{ delHtmlTags(scope.row.notice) }}</div>
				</template>
			</el-table-column>
			<el-table-column label="小白价格" align="center" prop="moneyOne" width="100">
				<template slot-scope="scope">
					<span class="price-tag">¥{{ scope.row.moneyOne }}</span>
				</template>
			</el-table-column>
			<el-table-column label="初级价格" align="center" prop="moneyTwo" width="100">
				<template slot-scope="scope">
					<span class="price-tag">¥{{ scope.row.moneyTwo }}</span>
				</template>
			</el-table-column>
			<el-table-column label="中级价格" align="center" prop="moneyThree" width="100">
				<template slot-scope="scope">
					<span class="price-tag">¥{{ scope.row.moneyThree }}</span>
				</template>
			</el-table-column>
			<el-table-column label="时长/小时" align="center" prop="minTime" width="100" />
			<el-table-column label="排序" align="center" prop="sort" width="80" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['service:service:edit']">
						修改
					</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['service:service:remove']">
						删除
					</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 分页 -->
		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" class="pagination-container" />

		<!-- 添加或修改服务管理对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false">
			<el-form ref="form" :model="form" :rules="rules" label-width="80px" class="dialog-form">
				<el-form-item label="图片" prop="image">
					<image-upload v-model="form.image" class="image-uploader" />
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="标签" prop="tag">
					<el-input v-model="form.tag" placeholder="请输入标签，多个标签用逗号分隔" />
				</el-form-item>
				<el-form-item label="服务类别" prop="type">
					<el-select v-model="form.type" placeholder="请选择服务类别">
						<el-option label="服务" value="1"></el-option>
						<el-option label="娱乐" value="2"></el-option>
						<el-option label="运动" value="3"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="选择人数" prop="number">
					<el-input-number v-model="form.number" :min="0" :step="1" placeholder="请输入选择人数"
						controls-position="right"></el-input-number>
				</el-form-item>
				<el-form-item label="排序" prop="sort">
					<el-input-number v-model="form.sort" :min="0" :step="1" placeholder="请输入排序"
						controls-position="right"></el-input-number>
				</el-form-item>
				<el-form-item label="项目详情">
					<editor v-model="form.details" :min-height="192" />
				</el-form-item>
				<el-form-item label="预定须知">
					<editor v-model="form.notice" :min-height="192" />
				</el-form-item>
				<el-form-item label="小白价格" prop="moneyOne">
					<el-input-number v-model="form.moneyOne" :min="0" :step="0.01" :precision="2" placeholder="请输入小白价格"
						controls-position="right" prefix="¥"></el-input-number>
				</el-form-item>
				<el-form-item label="初级价格" prop="moneyTwo">
					<el-input-number v-model="form.moneyTwo" :min="0" :step="0.01" :precision="2" placeholder="请输入初级价格"
						controls-position="right" prefix="¥"></el-input-number>
				</el-form-item>
				<el-form-item label="中级价格" prop="moneyThree">
					<el-input-number v-model="form.moneyThree" :min="0" :step="0.01" :precision="2"
						placeholder="请输入中级价格" controls-position="right" prefix="¥"></el-input-number>
				</el-form-item>
				<el-form-item label="时长/小时" prop="minTime">
					<el-input-number v-model="form.minTime" :min="0" :step="0.5" placeholder="请输入时长"
						controls-position="right" suffix="分钟"></el-input-number>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
				<el-button type="primary" @click="submitForm">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listService,
		getService,
		delService,
		addService,
		updateService
	} from "@/api/service/service"
	import {
		delHtmlTags
	} from "../../../utils/qt";

	export default {
		name: "Service",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 服务管理表格数据
				serviceList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					type: null,
					image: null,
					name: null,
					tag: null,
					number: null,
					sort: null,
					details: null,
					notice: null,
					moneyOne: null,
					moneyTwo: null,
					moneyThree: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					type: [{
						required: true,
						message: "请选择服务类别",
						trigger: "change"
					}],
					image: [{
						required: true,
						message: "请上传图片",
						trigger: "blur"
					}],
					name: [{
						required: true,
						message: "请输入名称",
						trigger: "blur"
					}],
					tag: [{
						required: true,
						message: "请输入标签，多个标签用逗号分隔",
						trigger: "blur"
					}],
					number: [{
						required: true,
						message: "请输入选择人数",
						trigger: "blur"
					}],
					sort: [{
						required: true,
						message: "请输入排序",
						trigger: "blur"
					}],
					details: [{
						required: true,
						message: "请输入项目详情",
						trigger: "blur"
					}],
					notice: [{
						required: true,
						message: "请输入预定须知",
						trigger: "blur"
					}],
					moneyOne: [{
						required: true,
						message: "请输入小白价格",
						trigger: "blur"
					}],
					moneyTwo: [{
						required: true,
						message: "请输入初级价格",
						trigger: "blur"
					}],
					moneyThree: [{
						required: true,
						message: "请输入中级价格",
						trigger: "blur"
					}],
					minTime: [{
						required: true,
						message: "请输入时长",
						trigger: "blur"
					}],
				}
			}
		},
		created() {
			this.getList()
		},
		methods: {
			// 处理富文本转纯文本
			delHtmlTags(row) {
				return delHtmlTags(row)
			},
			/** 查询服务管理列表 */
			getList() {
				this.loading = true
				listService(this.queryParams).then(response => {
					this.serviceList = response.rows
					this.total = response.total
					this.loading = false
				})
			},
			// 取消按钮
			cancel() {
				this.open = false
				this.reset()
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					type: null,
					image: null,
					name: null,
					tag: null,
					number: 0,
					sort: 0,
					details: null,
					notice: null,
					moneyOne: 0,
					moneyTwo: 0,
					moneyThree: 0,
					minTime: 0,
					createBy: null,
					createTime: null,
					updateBy: null,
					updateTime: null
				}
				this.resetForm("form")
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1
				this.getList()
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm")
				this.handleQuery()
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset()
				this.open = true
				this.title = "添加服务管理"
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset()
				const id = row.id || this.ids
				getService(id).then(response => {
					this.form = response.data
					this.open = true
					this.title = "修改服务管理"
				})
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateService(this.form).then(response => {
								this.$modal.msgSuccess("修改成功")
								this.open = false
								this.getList()
							})
						} else {
							addService(this.form).then(response => {
								this.$modal.msgSuccess("新增成功")
								this.open = false
								this.getList()
							})
						}
					}
				})
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids
				this.$modal.confirm(`是否确认删除服务管理编号为"${ids}"的数据项？`).then(() => {
					return delService(ids)
				}).then(() => {
					this.getList()
					this.$modal.msgSuccess("删除成功")
				}).catch(() => {})
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('service/service/export', {
					...this.queryParams
				}, `service_${new Date().getTime()}.xlsx`)
			}
		}
	}
</script>

<style scoped>
	/* 搜索区域样式 */
	.search-form {
		padding: 10px 15px;
		background-color: #f5f7fa;
		border-radius: 4px;
		margin-bottom: 10px;
	}

	.search-input,
	.search-select {
		width: 200px;
	}

	/* 操作栏样式 */
	.operation-bar {
		margin-bottom: 10px;
	}

	/* 表格样式 */
	.service-table {
		margin-bottom: 15px;
	}

	/* 标签样式 */
	.tag-item {
		margin: 0 4px 4px 0;
	}

	/* 图片样式 */
	.service-image {
		border-radius: 4px;
		cursor: pointer;
		transition: all 0.3s;
	}

	.service-image:hover {
		transform: scale(1.1);
	}

	/* 价格样式 */
	.price-tag {
		color: #f56c6c;
		font-weight: 500;
	}

	/* 省略文本样式 */
	.ellipsis-text {
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
		text-overflow: ellipsis;
		line-height: 1.5;
	}

	/* 分页样式 */
	.pagination-container {
		text-align: right;
		margin-top: 10px;
	}

	/* 对话框表单样式 */
	.dialog-form {
		max-height: 60vh;
		overflow-y: auto;
		padding-right: 10px;
	}

	/* 图片上传样式 */
	.image-uploader {
		width: 100%;
		max-width: 300px;
	}

	/* 对话框底部按钮样式 */
	::v-deep .dialog-footer {
		display: flex;
		justify-content: flex-end;
		gap: 10px;
	}
</style>